package com.atom.module.coroutine.ext
//
///**
// * All rights Reserved, Designed By www.rongdasoft.com
// * @version V1.0
// * @Title: IntrinsicsJvm
// * @Description:
// * @author: wuyongzheng
// * @date: 2022/1/10
// * @Copyright: 2022/1/10 www.rongdasoft.com Inc. All rights reserved.
// */
//
///*
// * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
// * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
// */
//
//@file:kotlin.jvm.JvmName("IntrinsicsKt")
//@file:kotlin.jvm.JvmMultifileClass
//@file:Suppress("UNCHECKED_CAST")
//
//import kotlin.coroutines.*
//import kotlin.coroutines.jvm.internal.*
//import kotlin.internal.InlineOnly
//
//
//@SinceKotlin("1.3")
//@InlineOnly
//public actual inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn(
//    completion: Continuation<T>
//): Any? = (this as Function1<Continuation<T>, Any?>).invoke(completion)
//
//@SinceKotlin("1.3")
//@InlineOnly
//public actual inline fun <R, T> (suspend R.() -> T).startCoroutineUninterceptedOrReturn(
//    receiver: R,
//    completion: Continuation<T>
//): Any? = (this as Function2<R, Continuation<T>, Any?>).invoke(receiver, completion)
//
//@InlineOnly
//internal actual inline fun <R, P, T> (suspend R.(P) -> T).startCoroutineUninterceptedOrReturn(
//    receiver: R,
//    param: P,
//    completion: Continuation<T>
//): Any? = (this as Function3<R, P, Continuation<T>, Any?>).invoke(receiver, param, completion)
//
//// JVM declarations
//
//@SinceKotlin("1.3")
//public actual fun <T> (suspend () -> T).createCoroutineUnintercepted(
//    completion: Continuation<T>
//): Continuation<Unit> {
//    val probeCompletion = probeCoroutineCreated(completion)
//    return if (this is BaseContinuationImpl)
//        create(probeCompletion)
//    else
//        createCoroutineFromSuspendFunction(probeCompletion) {
//            (this as Function1<Continuation<T>, Any?>).invoke(it)
//        }
//}
//
//@SinceKotlin("1.3")
//public actual fun <R, T> (suspend R.() -> T).createCoroutineUnintercepted(
//    receiver: R,
//    completion: Continuation<T>
//): Continuation<Unit> {
//    val probeCompletion = probeCoroutineCreated(completion)
//    return if (this is BaseContinuationImpl)
//        create(receiver, probeCompletion)
//    else {
//        createCoroutineFromSuspendFunction(probeCompletion) {
//            (this as Function2<R, Continuation<T>, Any?>).invoke(receiver, it)
//        }
//    }
//}
//
//@SinceKotlin("1.3")
//public actual fun <T> Continuation<T>.intercepted(): Continuation<T> =
//    (this as? ContinuationImpl)?.intercepted() ?: this
//
//
//@SinceKotlin("1.3")
//private inline fun <T> createCoroutineFromSuspendFunction(
//    completion: Continuation<T>,
//    crossinline block: (Continuation<T>) -> Any?
//): Continuation<Unit> {
//    val context = completion.context
//    // label == 0 when coroutine is not started yet (initially) or label == 1 when it was
//    return if (context === EmptyCoroutineContext)
//        object : RestrictedContinuationImpl(completion as Continuation<Any?>) {
//            private var label = 0
//
//            override fun invokeSuspend(result: Result<Any?>): Any? =
//                when (label) {
//                    0 -> {
//                        label = 1
//                        result.getOrThrow() // Rethrow exception if trying to start with exception (will be caught by BaseContinuationImpl.resumeWith
//                        block(this) // run the block, may return or suspend
//                    }
//                    1 -> {
//                        label = 2
//                        result.getOrThrow() // this is the result if the block had suspended
//                    }
//                    else -> error("This coroutine had already completed")
//                }
//        }
//    else
//        object : ContinuationImpl(completion as Continuation<Any?>, context) {
//            private var label = 0
//
//            override fun invokeSuspend(result: Result<Any?>): Any? =
//                when (label) {
//                    0 -> {
//                        label = 1
//                        result.getOrThrow() // Rethrow exception if trying to start with exception (will be caught by BaseContinuationImpl.resumeWith
//                        block(this) // run the block, may return or suspend
//                    }
//                    1 -> {
//                        label = 2
//                        result.getOrThrow() // this is the result if the block had suspended
//                    }
//                    else -> error("This coroutine had already completed")
//                }
//        }
//}